//
//  ZZBPopView.m
//  recordworktool_ios
//
//  Created by mc on 2017/12/7.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ZZBPopView.h"

@implementation ZZBPopView

#pragma mark - init
- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupDefault];
    }
    return self;
}

- (void)setupDefault {
    
    // 设置自身的属性
    self.bounds = [UIScreen mainScreen].bounds;
    
    // 背景半透明视图
    self.maskView = [UIButton buttonWithType:UIButtonTypeCustom];
    _maskView.frame = self.bounds;
    _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    _maskView.alpha = 0.0;
    [self addSubview:_maskView];
    [_maskView addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - tap
- (void)show {
    
    // 把本视图添加到窗口上
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    // 显示动画
    [self contentViewShowAnimation];
}
- (void)setType:(NSInteger)type{
    _type = type;
}


#pragma mark - animation
- (void)contentViewShowAnimation {
    _maskView.alpha = 0;
    CGFloat h = [[UIApplication sharedApplication] statusBarFrame].size.height;
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, h + 44 , SCREEN_WIDTH, 80)];
    [_maskView addSubview:backgroundView];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.alpha = 0;
    
    CGFloat left = 20;
    CGFloat magin = 15;
    CGFloat width = (SCREEN_WIDTH - left * 2 - magin * 2) / 3.0;
    CGFloat height = 35;
    NSArray *itemsAry = @[@"账户明细",@"充值明细",@"提现明细"];
    for (NSInteger i = 0; i < 3; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [backgroundView addSubview:button];
        button.frame = CGRectMake(left + (width + magin) * i, 20, width, height);
        [button setTitle:itemsAry[i] forState:UIControlStateNormal];
        button.layer.cornerRadius = 5;
        button.titleLabel.font = FONT(14);
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 100 + i;
        if (i == _type) {
            
            button.backgroundColor = BY_THEME_COLOR;
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
        }else{
            
            button.layer.borderColor = BY_THEME_COLOR.CGColor;
            button.layer.borderWidth = 1;
            button.backgroundColor = [UIColor whiteColor];
            [button setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
            
        }
        
    }
    
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _maskView.alpha = 1;
        
        backgroundView.alpha = 1;
    } completion:^(BOOL finished){
        
    }];
}
- (void)remove {
    
    // 隐藏动画
    [self contentViewRemoveAnimation];
    self.indexAtView(self.type);
}

- (void)contentViewRemoveAnimation {
    
  
    _maskView.alpha = 1;
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        _maskView.alpha = 0;
        
       
    } completion:^(BOOL finished){
        
        [self removeFromSuperview];
    }];
}


- (void)btnAction:(UIButton *)btn{
    
    [self remove];
    [self performSelector:@selector(perform1111:) withObject:[NSNumber numberWithInteger:btn.tag] afterDelay:0.5];
    
}

- (void)perform1111:(id)value{
    NSNumber *number = value;
    self.indexAtView(number.integerValue - 100);
}


@end
